package org.loofer.ext.component.info;

import android.net.NetworkInfo;

public class NetworkState {
    private static final int NETWORK_TYPE_1xRTT = 7;
    private static final int NETWORK_TYPE_CDMA = 4;
    private static final int NETWORK_TYPE_EDGE = 2;
    private static final int NETWORK_TYPE_EHRPD = 14;
    private static final int NETWORK_TYPE_EVDO_0 = 5;
    private static final int NETWORK_TYPE_EVDO_A = 6;
    private static final int NETWORK_TYPE_EVDO_B = 12;
    private static final int NETWORK_TYPE_GPRS = 1;
    private static final int NETWORK_TYPE_HSDPA = 8;
    private static final int NETWORK_TYPE_HSPA = 10;
    private static final int NETWORK_TYPE_HSPAP = 15;
    private static final int NETWORK_TYPE_HSUPA = 9;
    private static final int NETWORK_TYPE_IDEN = 11;
    private static final int NETWORK_TYPE_LTE = 13;
    private static final int NETWORK_TYPE_UMTS = 3;
    private static final int NETWORK_TYPE_UNKNOWN = 0;
    private static final NetworkState NONE = new NetworkState(false, null, AccessPoint.NONE, NetworkType.NONE);
    private AccessPoint accessPoint = AccessPoint.NONE;
    private String apnName = null;
    private boolean connected = false;
    private NetworkInfo moreInfo;
    private NetworkType type = NetworkType.NONE;

    private NetworkState(boolean conn, String apn, AccessPoint ap, NetworkType tp) {
        setConnected(conn);
        setApnName(apn);
        setAccessPoint(ap);
        setType(tp);
    }

    private NetworkState() {
    }

    public static NetworkState fromNetworkInfo(NetworkInfo info) {
        if (info == null) {
            return NONE;
        }
        NetworkState state = new NetworkState();
        state.setConnected(info.isConnected());
        state.setApnName(info.getExtraInfo());
        state.setAccessPoint(AccessPoint.forName(state.getApnName()));
        switch (info.getType()) {
            case 0:
            case 2:
            case 3:
            case 4:
            case 5:
                state.setType(convertMobileType(info.getSubtype()));
                break;
            case 1:
                state.setType(NetworkType.WIFI);
                break;
            case 9:
                state.setType(NetworkType.ETHERNET);
                break;
            default:
                state.setType(NetworkType.OTHERS);
                break;
        }
        state.setMoreInfo(info);
        return state;
    }

    private static NetworkType convertMobileType(int subType) {
        switch (subType) {
            case 0:
            case 1:
            case 2:
            case 4:
            case 7:
            case 11:
                return NetworkType.MOBILE_2G;
            case 3:
            case 5:
            case 6:
            case 8:
            case 9:
            case 10:
            case 12:
            case 14:
            case 15:
                return NetworkType.MOBILE_3G;
            case 13:
                return NetworkType.MOBILE_4G;
            default:
                return NetworkType.OTHERS;
        }
    }

    @Deprecated
    private static boolean is3GMobileType(int type) {
        NetworkType t = convertMobileType(type);
        return t == NetworkType.MOBILE_3G || t == NetworkType.MOBILE_4G;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        boolean z = (o instanceof NetworkState) && ((NetworkState) o).isConnected() == isConnected() && ((NetworkState) o).getType().equals(getType()) && ((NetworkState) o).getApnName().equals(getApnName());
        return z;
    }

    public boolean isConnected() {
        return this.connected;
    }

    public void setConnected(boolean connected) {
        this.connected = connected;
    }

    public boolean isAvailable() {
        return this.connected;
    }

    public String getApnName() {
        if (this.apnName == null) {
            return "";
        }
        return this.apnName;
    }

    public void setApnName(String apnName) {
        this.apnName = apnName;
    }

    public NetworkType getType() {
        return this.type;
    }

    public void setType(NetworkType type) {
        this.type = type;
    }

    public AccessPoint getAccessPoint() {
        return this.accessPoint;
    }

    public void setAccessPoint(AccessPoint accessPoint) {
        this.accessPoint = accessPoint;
    }

    public NetworkInfo getMoreInfo() {
        return this.moreInfo;
    }

    public void setMoreInfo(NetworkInfo moreInfo) {
        this.moreInfo = moreInfo;
    }

    public String toString() {
        return "NetworkState [connected=" + this.connected + ", apnName=" + this.apnName + ", type=" + this.type + ", accessPoint=" + this.accessPoint + "]";
    }
}
